<?php

class WorkerTypeController extends Controller
{


    /*
    * 获取列表分页
    *
    */
    public function actionGetListPaging()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            $searchsql = Util::initSearchSql();


            //获取count
            $totalcount = WorkerTypes::model()->count($searchsql);

                //排序
                $searchsql = $searchsql . " order by priority asc";

                //加上分页控制
                $searchsql = Util::addLimitSql($searchsql);

                //获取list
                $list = WorkerTypes::model()->findAll($searchsql);


                Util::responseList($totalcount, $list);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
    * 添加
    *
    */
    public function actionAdd()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $name = Util::getParamNotEmpty("name");
            $priority = Util::getParam("priority","1");

            //名称重复判断

            $obj = WorkerTypes::model()->find("name=:name ",
                array(":name" => $name));

            if(empty($obj)==true)
            {
                //添加
                $obj = new WorkerTypes();
                Util::initObjData($obj);
                $obj->name = $name;
                $obj->priority = $priority;

            }
            else
            {
                if(Util::isDelete($obj)==false)
                    throw new Exception("该名称已存在");

                Util::recoverObjData($obj);
            }

            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 删除
    */
    public function actionDelete()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");



            //删除
            $obj = WorkerTypes ::model()->findByPK($obj_id);

            Util::checkExistAndNotDelete($obj);

            Util::deleteObjData($obj);

            Util::saveObjData($obj);



            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


                } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 修改
    *
    */
    public function actionUpdate()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");
            $name = Util::getParamNotEmpty("name");
            $priority = Util::getParam("priority","1");


            //验证存在
            $obj = WorkerTypes::model()->findByPK($obj_id);

            Util::checkExistAndNotDelete($obj);


            //名称重复判断

            $oldobj = WorkerTypes::model()->find("name=:name ",
                array(":name" => $name));

            if(empty($oldobj)==false)
                if($oldobj->type_id != $obj_id)
                    throw new Exception("名称与其他类型重复");

            //修改信息

            $obj->name = $name;
            $obj->priority= $priority;

            //保存
            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


}